﻿using MediatR;
using RedDragon.Common.Domain.produceplan;
using RedDragon.Common.Infrastructure.Interface;
using T.Common.Domain.Common;

namespace RedDragon.RBAC.API.Application.Common.produceplanW
{
    public class produceplanApprovalHandler : IRequestHandler<produceplanApprovalCommon, Result>
    {
        public readonly IBaseRepository<produceplan> produceplanRepository;

        public produceplanApprovalHandler(IBaseRepository<produceplan> produceplanRepository)
        {
            this.produceplanRepository = produceplanRepository;
        }

        public async Task<Result> Handle(produceplanApprovalCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = 0, Msg = "主生产计划审批状态修改" };
            try
            {
                if (request.produceplanNumber != null)
                {
                    var id = produceplanRepository.Query().Where(x => x.produceplanNumber == request.produceplanNumber).FirstOrDefault().produceplanId;
                    var list = produceplanRepository.Query().Where(x => x.produceplanId == id);
                    foreach (var item in list)
                    {
                        item.produceplanAudit =request.ChangeNum;
                        produceplanRepository.Update(item);
                    }
                    result.Msg = "主生产计划审批状态修改成功";
                }
                else
                {
                    result.Msg = "编号为空无法进入方法";
                }
              


            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
